package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_DELETE, desc = "解散联盟,进入冷静期")
public class DeleteUnionCmd extends AbstractCommand {

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		// 判断权限 族长
		String unionUid = player.getUserInfo().getUnionUid();
		if (StringUtils.isNullOrEmpty(unionUid)) {
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
		if (null == unionInfo) {
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		long nowTime = System.currentTimeMillis() / 1000;
		if (unionInfo.getDeleteCoolingOffTime() + GameConfig.UNION_DELETE_COOLING_OFF_CD > nowTime) {
			ret = GameErrorCode.E_UNION_IN_DELETE_COOLING_OFF_TIME;
			return;
		}

		if (unionInfo.getMasterUserId() == player.getUserId()) {
			ret = player.getModule(CrossUnionModule.class).canDeleteUnion();
			player.sendPacket(Protocol.C_UNION_DELETE, UnionProto.CrossUnionDeleteReqMsg.newBuilder().setCanDeleteUnionRet(ret));
			ret = 0;
		} else {
			ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0) {
			UnionProto.UnionDeleteRespMsg.Builder respMsg = UnionProto.UnionDeleteRespMsg.newBuilder();
			respMsg.setRet(ret);
			respMsg.setDeleteCoolingOffTime(0);
			player.sendPacket(Protocol.U_UNION_DELETE, respMsg);
		}
	}

}
